<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Snowflake Id Demo</title>
  <script src="../snowflake.js"></script>
</head>

<body>
  默认
  <ul id="default-id">
  </ul>
  <hr />
  定制化
  <ul id="customer-id">
  </ul>
  <script>
    defaultSnowflake()
    customerSnowflake()
    testSpeed(600000)
    testSpeed(700000)
    // 默认雪花ID
    function defaultSnowflake () {
      const snowflake = new Snowflake()
      // 返回单个id 用数组形式返回
      // const id1 = snowflake.nextId(1, false)
      // console.log('id1:', id1)
      // 返回单个id 默认用字符串形式返回
      const id = snowflake.nextId()
      console.log('nextId:', id)
      console.log('timestamp:', snowflake.parseIdGenerateTime(id))
      console.log('time:', new Date(snowflake.parseIdGenerateTime(id)))
      console.log('datacenter:', snowflake.parseDataCenterId(id))
      console.log('workId:', snowflake.parseWorkerId(id))
      const node = document.createElement("li")
      const textnode = document.createTextNode(id);
      node.appendChild(textnode);
      document.getElementById('default-id').appendChild(node)
    }
    // 定制化雪花ID
    function customerSnowflake () {
      const snowflake = new Snowflake(31, 31, true, 1288834974657)
      const ids = snowflake.nextId(10)
      console.log('nextId:', ids)
      console.log('timestamp:', snowflake.parseIdGenerateTime(ids[0]))
      console.log('time:', new Date(snowflake.parseIdGenerateTime(ids[0])))
      console.log('datacenter:', snowflake.parseDataCenterId(ids[0]))
      console.log('workId:', snowflake.parseWorkerId(ids[0]))
      ids.forEach(e => {
        const node = document.createElement("li")
        const textnode = document.createTextNode(e);
        node.appendChild(textnode);
        document.getElementById('customer-id').appendChild(node)
      })
    }

    function testSpeed (nums) {
      const start = new Date().getTime()
      const snowflake = new Snowflake()
      snowflake.nextId(nums)
      const cost = new Date().getTime() - start
      console.log(`[${nums}] cost: [${cost}]`)
    }
  </script>
</body>

</html>